package com.aggron.service;

import com.aggron.dao.SysAclMapper;
import com.aggron.dao.SysRoleAclMapper;
import com.aggron.dao.SysRoleUserMapper;
import com.aggron.entity.SysAcl;
import com.aggron.util.CollectionsUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色、权限点、用户的service
 * @author  liuhuan on 2018/6/6.
 */
@Service
public class SysCoreService {
    @Autowired
    private SysAclMapper sysAclMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    /**
     * 获取当前用户所拥有的权限点
     * @return
     */
    public List<SysAcl> getCurrentUserAclList(){
        //RequestHolder.getCurrentUser().getId()
        Integer userId=1 ;
        return getUserAclList(userId);
    }

    /**
     * 获取角色已拥有的权限点
     * @param roleId
     * @return
     */
    public List<SysAcl> getRoleAclList(Integer roleId){
        List<Integer> aclIdList= sysRoleAclMapper.getAclIdListByRoleIdList(Lists.newArrayList(roleId));
        if(CollectionsUtil.isEmpty(aclIdList)){
            return Lists.newArrayList();
        }
        return sysAclMapper.getSysAclListByAclIdList(aclIdList);
    }

    /**
     * 通过用户id获取用户所拥有的权限点
     * @param userId
     * @return
     */
    public List<SysAcl> getUserAclList(Integer userId){
        if(isSuperAdmin()){
            return sysAclMapper.selectList(null);
        }
        List<Integer> roleIdList=sysRoleUserMapper.getRoleIdListByUserId(userId);
        if(CollectionsUtil.isEmpty(roleIdList)){
            return Lists.newArrayList();
        }
        List<Integer> userAclIdList=sysRoleAclMapper.getAclIdListByRoleIdList(roleIdList);
        if(CollectionsUtil.isEmpty(userAclIdList)){
            return Lists.newArrayList();
        }
        return sysAclMapper.getSysAclListByAclIdList(userAclIdList);

    }

    /**
     * 是否超级用户
     * @return
     */
    public  boolean isSuperAdmin(){
        //TODO
        return true;
    }
}
